﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;
using Persistencia;

namespace LogicaNegocios.Manager
{

    public class contentDataLog
    {
        private DateTime date;
        private string info;

        public contentDataLog(DateTime date, string data)
        {
            this.date = date;
            this.info = data;
        }

        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        public string Info
        {
            get { return info; }
            set { info = value; }
        }
    }

    public class ReporteManager
    {
        #region Singleton Code

        private static ReporteManager _manager;

        private ReporteManager() { }

        public static ReporteManager GetManager()
        {
            if (_manager == null)
            {
                _manager = new ReporteManager();
            }

            return _manager;
        }

        #endregion

        public DataSet GetStoreProcedureCalendar(DateTime fechaStart)
        {
            ReportePersistencia rp = new ReportePersistencia();
            return rp.GetStoreProcedureCalendar(fechaStart);
        }


        public DataSet GetStoreProcedureMedallero()
        {
            ReportePersistencia rp = new ReportePersistencia();
            return rp.GetStoreProcedureMedallero();
        }
        
        private static string CopyFile(string path, string fileName)
        {
            string destino = string.Empty;
            try
            {
                string dir = "Temp\\";
                string origen = path + fileName;
                destino = path + dir + fileName + "_temp";
                if (!System.IO.Directory.Exists(path + dir))
                {
                    System.IO.Directory.CreateDirectory(path + dir);
                }

                System.IO.File.Copy(origen, destino, true);
            }
            catch { destino = ""; }
            return destino;
        }

        private List<contentDataLog> GenerateDataList(string pathFileLog)
        {
            string fileNeme = "aplicacion.txt";
            StreamReader objReader = null;
            List<contentDataLog> info = new List<contentDataLog>();
            try
            {
                string logFileRead = CopyFile(pathFileLog, fileNeme);
                if (!string.IsNullOrEmpty(logFileRead))
                {
                    objReader = new StreamReader(logFileRead, Encoding.UTF8);
                    string sLine = "";
                    string dateAux = "";
                    string dataAux = "";
                    int fechaMes = 0;
                    int fechaAnio = 0;
                    int fechaDia = 0;
                    int H = 0;
                    int m = 0;
                    int s = 0;
                    string[] aux;
                    while (sLine != null)
                    {
                        sLine = objReader.ReadLine();
                        if (sLine != null)
                        {
                            aux = sLine.Split('|');
                            dateAux = aux[0];
                            dataAux = aux[1];
                            DateTime fecha;
                            if (!string.IsNullOrEmpty(dateAux) && !string.IsNullOrEmpty(dataAux))
                            {
                                //fecha
                                fechaAnio = int.Parse(dateAux.Substring(0, 4));
                                fechaMes = int.Parse(dateAux.Substring(5, 2));
                                fechaDia = int.Parse(dateAux.Substring(8, 2));
                                H = int.Parse(dateAux.Substring(11, 2));
                                m = int.Parse(dateAux.Substring(14, 2));
                                s = int.Parse(dateAux.Substring(17, 2));
                                fecha = new DateTime(fechaAnio, fechaMes, fechaDia, H, m, s);
                                info.Add(new contentDataLog(fecha, dataAux));
                            }
                        }

                    }
                }
            }
            catch { }
            finally
            {
                if (objReader != null)
                    objReader.Close();
            }
            return info;

        }

        public List<contentDataLog> logEntradaUsuarios(DateTime fechaIni, DateTime fechaFin, string pathFileLog)
        {
            List<contentDataLog> info = GenerateDataList(pathFileLog);
            List<contentDataLog> ret = new List<contentDataLog>();

            if (info.Count > 0)
            {
                fechaFin = fechaFin.AddDays(1);
                ret = info.Where(i => i.Date >= fechaIni && i.Date <= fechaFin).ToList();
            }
            return ret;
        }
    }
}
